from numpy import *
import profile
#==== ==== ==== ====
# Return all the indexes of a charecters c in text
#==== ==== ==== ====
# 1000x = 0.007 CPU seconds
def allindex(text, c):
    return [i for i, char in enumerate(text) if char == c]


def s(a, b):
    ave = ((a + b) / 2.0)
    return math.sqrt(  (((a - ave) ** 2) + ((b - ave) ** 2)) / 2  )

#return (((a - ave) ** 2) + ((b - ave) ** 2)) / 2
#print s(1,2)


def path_std(now, parent, child):
    if parent == -1:
        return 0
    return now + s(now, child - parent)

    #return [now + array([now, child - parent]).std(), 0][parent == -1]
#==== ==== ==== ====
# FINDS ALL the Indexes of s in t STRING THAT IN NON-CONTINOUS
# returns standart deviation of the paths
#==== ==== ==== ====
# 1000x = 0.548 CPU seconds
# s = 'dsa', t = 'ioDnDSiiA'
# Usins arrays of indexes
# foo(-1, [[0], [1], [2, 6], [3, 11], [4, 12]])
# 'd': [2, 4], 's': [5]...
# It build a path do the last letter :)
def sskFindOut(k, order, childs):
    allSubStrings = [] #array([], dtype='float64') --> UnboundLocalError: local variable 'allSubStrings' referenced before assignment
    childs = childs
    len_childs = len(childs)
    k = k
    order = order
    def sskFind(parent, index_childs=0, pot=array([], dtype='int32'), depth=0): # pot=0
        # Vrni podatke
        #print " " * depth, "== %s ==" % (parent)
        #print " " * depth, pot
        #print " " * depth, "childs: %s, pot: %s" % (childs, pot)
        #print " " * depth, "allSubStrings: %s" % allSubStrings
        if parent == -99:
            #print "parent == True"
            #print parent
            return allSubStrings
        # If there are still childs
        if len_childs > index_childs: # 5 > 4 npr...
            for i in childs[index_childs]:
                #print " " * depth, "Child:", i
                # The path is upwards only
                if parent < i:
                    if i - parent > (k * order)  and parent != -1:
                        #print " " * depth, "Prevelik skos!!!", pot, parent, i
                        return
                    else:
                        # Go to all the childs with the already taken path
                        #   + current child
                        sskFind(i, index_childs + 1, append(pot, i), depth + 4) #path_std(pot, parent, i) #path_std(pot, parent, i)
                #elif parent != -1:
                #    break #" " * depth, "Blah", parent, i

        else:
            #print " " * depth, " FOUND: pot:", pot#, "std():" , pot.std(), pot.std() ** 2
            #allSubStrings = append(allSubStrings, pot.std())
            allSubStrings.append(pot.std()) #.std()

    return sskFind

# TEST sskFindOut
def testSskFind():
    k = 5
    order = 1
    foo = sskFindOut(k, order, [[10], [11], [5, 12, 15], [13, 17], [14, 19]])
    foo(-1, 0)
    return foo(-99)

def main():
    # TEST allindex
    print "allindex:", allindex("sdfsdf d", "d")
    print "allindex: [1, 4, 7]"

    print "*" * 10, "profile:"
    profile.run("[allindex('sdfsdf d', 'd') for i in range(1000)]")



    print "*" * 10
    print "sskFind: ", testSskFind()
    print "sskFind: [0.9375, 3.3125, 2.5625]"

    print "*" * 10, "profile:"
    #profile.run("[testSskFind() for i in range(1000)]")

if __name__ == '__main__':
    main()